<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>重置密码 - 智能代码评估系统</title>
    {% load static %}
    <link href="{% static 'vendor/bootstrap/css/bootstrap.min.css' %}" rel="stylesheet">
    <link href="{% static 'vendor/fontawesome-free/css/all.min.css' %}" rel="stylesheet">
    <style>
        :root {
            --primary-color: #3498db;
            --secondary-color: #2ecc71;
            --dark-color: #34495e;
            --light-color: #ecf0f1;
        }

        body {
            background-color: #f8f9fa;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            min-height: 100vh;
            margin: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }

        .reset-container {
            width: 100%;
            max-width: 400px;
            padding: 30px;
            background: white;
            border-radius: 10px;
            box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
            text-align: center;
        }

        .reset-header {
            margin-bottom: 30px;
        }

        .reset-header h2 {
            color: var(--dark-color);
            font-weight: 600;
            margin-bottom: 10px;
        }

        .reset-header p {
            color: #7f8c8d;
            font-size: 14px;
            margin-bottom: 20px;
        }

        .form-group {
            display: flex;
            flex-direction: column;
            align-items: center;
            margin-bottom: 15px;
            width: 100%;
        }

        .form-control {
            height: 45px;
            border-radius: 5px;
            border: 1px solid #ddd;
            padding-left: 15px;
            width: 100%;
            max-width: 300px;
            box-sizing: border-box;
        }

        .form-control:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 0.2rem rgba(52, 152, 219, 0.25);
        }

        .btn-reset {
            background: linear-gradient(45deg, var(--primary-color), var(--secondary-color));
            border: none;
            color: white;
            height: 45px;
            border-radius: 5px;
            font-weight: 500;
            width: 100%;
            max-width: 300px;
            transition: all 0.3s;
            margin-top: 10px;
        }

        .btn-reset:hover {
            background: linear-gradient(45deg, #2980b9, #27ae60);
            transform: translateY(-2px);
        }

        .form-footer {
            margin-top: 20px;
            font-size: 14px;
            width: 100%;
            max-width: 300px;
            margin-left: auto;
            margin-right: auto;
            text-align: center;
        }

        .form-footer a {
            color: var(--primary-color);
            text-decoration: none;
        }

        .alert {
            max-width: 300px;
            margin: 0 auto 15px;
            text-align: left;
        }

        .step {
            display: none;
        }

        .step.active {
            display: block;
        }

        .password-strength {
            width: 100%;
            max-width: 300px;
            margin: 5px auto;
            height: 5px;
            background: #eee;
            border-radius: 3px;
            overflow: hidden;
        }

        .password-strength-bar {
            height: 100%;
            width: 0%;
            background: #e74c3c;
            transition: width 0.3s, background 0.3s;
        }

        .password-hint {
            font-size: 12px;
            color: #7f8c8d;
            margin-top: 5px;
            width: 100%;
            max-width: 300px;
            margin-left: auto;
            margin-right: auto;
            text-align: left;
        }

        .success-icon {
            color: #2ecc71;
            font-size: 80px;
            margin: 20px 0;
        }
    </style>
</head>
<body>
    <div class="reset-container">
        <!-- 第一步：验证用户信息 -->
        <div id="step-verify" class="step active">
            <div class="reset-header">
                <h2><i class="fas fa-key"></i> 重置密码</h2>
                <p>请输入您的用户名和注册邮箱</p>
            </div>

            <form id="verify-form" method="post" action="{% url 'verify_user' %}">
                {% csrf_token %}
                {% if error_message %}
                <div class="alert alert-danger">{{ error_message }}</div>
                {% endif %}

                <div class="form-group">
                    <input type="text" class="form-control" id="username" name="username" placeholder="用户名" required>
                </div>

                <div class="form-group">
                    <input type="email" class="form-control" id="email" name="email" placeholder="注册邮箱" required>
                </div>

                <div class="form-group">
                    <button type="submit" id="verify-btn" class="btn btn-reset">验证信息</button>
                </div>

                <div class="form-footer">
                    <a href="/login">返回登录</a>
                </div>
            </form>
        </div>

        <!-- 第二步：设置新密码 -->
        <div id="step-reset" class="step">
            <div class="reset-header">
                <h2><i class="fas fa-lock"></i> 设置新密码</h2>
                <p>请为账号 <span id="reset-username" style="font-weight: bold;"></span> 设置新密码</p>
            </div>

            <form id="reset-form" method="post" action="/reset/">
                {% csrf_token %}
                <input type="hidden" id="reset-username-field" name="username">

                <div class="form-group">
                    <input type="password" class="form-control" id="new-password" name="new_password" placeholder="新密码" required>
                    <div class="password-strength">
                        <div class="password-strength-bar" id="password-strength-bar"></div>
                    </div>
                    <div class="password-hint">密码至少需要6个字符</div>
                </div>

                <div class="form-group">
                    <input type="password" class="form-control" id="confirm-password" name="confirm_password" placeholder="确认新密码" required>
                </div>

                <div class="form-group">
                    <button type="submit" id="reset-btn" class="btn btn-reset">重置密码</button>
                </div>

                <div class="form-footer">
                    <a href="#" id="back-to-verify">返回上一步</a>
                </div>
            </form>
        </div>

        <!-- 第三步：重置成功 -->
        <div id="step-success" class="step">
            <div class="reset-header">
                <h2><i class="fas fa-check-circle"></i> 密码重置成功</h2>
                <p>您的密码已成功更新！</p>
            </div>

            <div class="success-icon">
                <i class="fas fa-check-circle"></i>
            </div>

            <div class="form-group">
                <a href="/login" class="btn btn-reset">返回登录</a>
            </div>
        </div>
    </div>

    <script src="{% static 'vendor/jquery/jquery.min.js' %}"></script>
    <script>
        $(document).ready(function() {
            // 如果直接从URL进入且有错误消息，显示第一步
            if ($(".alert-danger").length) {
                goToStep('step-verify');
            }

            // 验证表单提交
            $("#verify-form").on('submit', function(e) {
                e.preventDefault();
                const form = $(this);

                $.ajax({
                    type: "POST",
                    url: form.attr('action'),
                    data: form.serialize(),
                    success: function(response) {
                        if (response.success) {
                            $("#reset-username").text(response.username);
                            $("#reset-username-field").val(response.username);
                            goToStep('step-reset');
                        } else {
                            alert(response.message || '验证失败，请检查用户名和邮箱');
                        }
                    },
                    error: function() {
                        alert('请求失败，请稍后重试');
                    }
                });
            });

            // 密码重置表单提交
            $("#reset-form").on('submit', function(e) {
                e.preventDefault();
                const form = $(this);
                const newPassword = $("#new-password").val();
                const confirmPassword = $("#confirm-password").val();

                if (newPassword.length < 6) {
                    alert('密码至少需要6个字符！');
                    return;
                }

                if (newPassword !== confirmPassword) {
                    alert('两次输入的密码不匹配！');
                    return;
                }

                $.ajax({
                    type: "POST",
                    url: form.attr('action'),
                    data: form.serialize(),
                    success: function(response) {
                        if (response.success) {
                            goToStep('step-success');
                        } else {
                            alert(response.message || '密码重置失败');
                        }
                    },
                    error: function() {
                        alert('请求失败，请稍后重试');
                    }
                });
            });

            // 密码强度检测
            $("#new-password").on('input', function() {
                const password = $(this).val();
                const strengthBar = $("#password-strength-bar");
                const resetBtn = $("#reset-btn");

                // 简单密码强度检测
                let strength = 0;
                if (password.length > 0) strength = 1;
                if (password.length >= 6) strength = 2;
                if (password.length >= 8 && /[A-Z]/.test(password)) strength = 3;
                if (password.length >= 10 && /[A-Z]/.test(password) && /[0-9]/.test(password)) strength = 4;

                // 更新强度条
                const width = strength * 25;
                strengthBar.css('width', width + '%');

                // 更新颜色
                if (strength <= 1) {
                    strengthBar.css('background-color', '#e74c3c');
                } else if (strength === 2) {
                    strengthBar.css('background-color', '#f39c12');
                } else {
                    strengthBar.css('background-color', '#2ecc71');
                }

                // 启用/禁用重置按钮
                resetBtn.prop('disabled', password.length < 6);
            });

            // 确认密码匹配检测
            $("#confirm-password").on('input', function() {
                const newPassword = $("#new-password").val();
                const confirmPassword = $(this).val();

                if (confirmPassword && newPassword !== confirmPassword) {
                    this.setCustomValidity('两次输入的密码不匹配');
                } else {
                    this.setCustomValidity('');
                }
            });

            // 返回验证步骤
            $("#back-to-verify").on('click', function(e) {
                e.preventDefault();
                goToStep('step-verify');
            });
        });

        // 切换步骤
        function goToStep(stepId) {
            $(".step").removeClass('active');
            $("#" + stepId).addClass('active');
        }
    </script>
</body>
</html>